//
//  DetailViewController.m
//  ICEViewPrototype
//
//  Created by Aurélien Ammeloot on 13/07/2010.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "DetailViewController.h"
#import "RootViewController.h"
#import "ICEFullScreenTextVC.h"
#import "ICEFullImageViewController.h"
#import "ICESearchViewController.h"
#import "TapDetectingImageView.h"
#import "ICEStory.h"

#import "ICEPageViewController.h"

#define TOOLBAR_HEIGHT toolbar.frame.size.height

@interface DetailViewController () <UIWebViewDelegate, TapDetectingImageViewDelegate>
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;
@end



@implementation DetailViewController

@synthesize toolbar, popoverController, detailItem, detailDescriptionLabel;

#pragma mark -
#pragma mark Managing the detail item

/*
 When setting the detail item, update the view and dismiss the popover controller if it's showing.
 */
- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
        
        // Update the view.
        [self configureView];
    }

    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }        
}


- (void)configureView {
    // Update the user interface for the detail item.
    detailDescriptionLabel.text = [detailItem description];   
}


#pragma mark -
#pragma mark Split view support

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
    
    barButtonItem.title = @"Navigation";
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = pc;
}


// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items removeObjectAtIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Rotation support

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


#pragma mark -
#pragma mark View lifecycle


 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//_webViewHasAlreadyLoaded = NO;
	
	// Load webview sample content
	
//	NSString *path = [[NSBundle mainBundle] pathForResource:@"page2" ofType:@"html" inDirectory:@"Sample"];
//	NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
	
	
	//NSURL *url = [NSURL URLWithString:@"http://www.mondedemerde.eu/ice/page1.html"];
	//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//	[_webView loadRequest:request];
//	[_webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
	
	_pageViewerController = [[ICEPageViewController alloc] initWithNibName:@"ICEPageViewController" bundle:[NSBundle mainBundle]];
	_pageViewerController.view.frame = CGRectMake(0., toolbar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
	[self.view addSubview:_pageViewerController.view];
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Memory management

/*
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
*/

- (void)dealloc {
    [popoverController release];
    [toolbar release];
    
    [detailItem release];
    [detailDescriptionLabel release];
	[_pageViewerController release];
    [super dealloc];
}

#pragma mark -
#pragma mark TapDetectingImageViewDelegate

#pragma mark -
#pragma mark Orientation Changes

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	// Resize the subview when the orientation changes
	// Can be decomposed in several steps, and animated (further development)
	_pageViewerController.view.frame = CGRectMake(0., TOOLBAR_HEIGHT, self.view.bounds.size.width, self.view.bounds.size.height - TOOLBAR_HEIGHT);
	[_pageViewerController updateOrientation];
	_pageViewerController.detailViewController = self;
}

#pragma mark -
#pragma mark Event handlers

- (IBAction)showSharingActions:(id)sender
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Sharing" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Twitter",@"Email",@"Facebook",nil];
	[actionSheet showFromBarButtonItem:sender animated:YES];
}

- (IBAction)showSearchBox:(id)sender
{
	ICESearchViewController *searchVC = [[ICESearchViewController alloc] initWithNibName:@"ICESearchViewController" bundle:[NSBundle mainBundle]];
	UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:searchVC];
	[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

#pragma mark -
#pragma mark Story getter / setter

- (ICEStory *)story
{
	return _story;
}

- (void)setStory:(ICEStory *)story
{
	if (_story != nil) {
		[_story release];
	}
	_story = [story retain];
	[_pageViewerController setStory:_story];
}

@end
